#!/usr/bin/python3
# /usr/bin/env python
# -*- coding: UTF-8 -*-

from aip import AipOcr
from zhtools.langconv import *
from log.Logger import Logger

""" 你的 APPID AK SK """
APP_ID = '16314200'
API_KEY = '7GgOuavvQadW0vWOXYYhmaB8'
SECRET_KEY = 'Yeu8YVOHOjkLw2mTyQbfLKNibIGWTll7'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
filePath = 'D://无标题.png'


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


def traditional_2_simplified(sentence):
    '''
    将sentence中的繁体字转为简体字
    :param sentence: 待转换的句子
    :return: 将句子中繁体字转换为简体字之后的句子
    '''
    sentence = Converter('zh-hans').convert(sentence)
    return sentence


log = Logger("D:/logs/ocr.log", level='debug')

image = get_file_content(filePath)
""" 调用通用文字识别（高精度版） """
client.basicAccurate(image);

""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别（高精度版） """
result = client.basicAccurate(image, options)
lines = result.get("words_result")
res_str = ''
for line in lines:
    res_str = res_str + line.get('words')
    # log.logger.info("Original OCR text： " + res_str)
    res_str = traditional_2_simplified(res_str)
    # log.logger.info("After Complex to Simple： " + res_str)
print(res_str)
